package com.aj.activity.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aj.activity.R;
import com.aj.databases.entry.FamilySpace;
import com.aj.utils.PortraitLoaderUtil;

public class FamilyUserAdapter extends BaseAdapter {

	private List<FamilySpace> familySpaceList;
	private Context context;
	private HashMap<Integer, View> viewCache = new HashMap<Integer, View>();

	public FamilyUserAdapter(Context context, List<FamilySpace> familySpaceList) {
		this.context = context;
		this.familySpaceList = familySpaceList;
	}

	@Override
	public int getCount() {
		return familySpaceList.size();
	}

	@Override
	public Object getItem(int position) {
		return familySpaceList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		ImageView head_img;
		TextView name_text;
		TextView ajnumber_text;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = viewCache.get(position);
		FamilySpace familySpace = familySpaceList.get(position);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_familyspace_photo, null);
			viewHolder = new ViewHolder();
			viewHolder.ajnumber_text = (TextView) convertView
					.findViewById(R.id.ajnumber_text);
			viewHolder.name_text = (TextView) convertView
					.findViewById(R.id.name_text);
			viewHolder.head_img = (ImageView) convertView
					.findViewById(R.id.head_img);
			convertView.setTag(viewHolder);
			viewCache.put(position, convertView);
			PortraitLoaderUtil.load(context, viewHolder.head_img, null,
					familySpace.getPhoto(), R.drawable.head_icon,
					R.drawable.head_icon);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.name_text.setText(familySpace.getNickName());
		viewHolder.ajnumber_text.setText("(" + familySpace.getAjNo() + ")");

		return convertView;
	}

}
